home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTList.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  3.1 KB  |  145 lines

  1. /*    A small List class                          HTList.c
  2. **    ==================
  3. **
  4. **    A list is represented as a sequence of linked nodes of type HTList.
  5. **    The first node is a header which contains no object.
  6. **    New nodes are inserted between the header and the rest of the list.
  7. */
  8.  
  9. #include "HTList.h"
  10.  
  11. #include <stdio.h>                /* joe@athena, TBL 921019 */
  12.  
  13. HTList * HTList_new NOARGS
  14. {
  15.   HTList *newList = (HTList *)malloc (sizeof (HTList));
  16.   if (newList == NULL) outofmem(__FILE__, "HTList_new");
  17.   newList->object = NULL;
  18.   newList->next = NULL;
  19.   return newList;
  20. }
  21.  
  22. void HTList_delete ARGS1(HTList *,me)
  23. {
  24.   HTList *current;
  25.   while (current = me) {
  26.     me = me->next;
  27.     free (current);
  28.   }
  29. }
  30.  
  31. void HTList_addObject ARGS2(HTList *,me, void *,newObject)
  32. {
  33.   if (me) {
  34.     HTList *newNode = (HTList *)malloc (sizeof (HTList));
  35.     if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
  36.     newNode->object = newObject;
  37.     newNode->next = me->next;
  38.     me->next = newNode;
  39.   }
  40.   else
  41.     if (TRACE) fprintf(stderr,
  42.         "HTList: Trying to add object %p to a nonexisting list\n",
  43.                newObject);
  44. }
  45.  
  46. void HTList_addObjectAtEnd ARGS2(HTList *,me, void *,newObject)
  47. {
  48.   if (me) 
  49.     {
  50.       HTList *newNode = (HTList *)malloc (sizeof (HTList));
  51.       if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
  52.       newNode->object = newObject;
  53.       newNode->next = NULL;
  54.       while (me->next) 
  55.         me = me->next;
  56.       me->next = newNode;
  57.     }
  58.   else
  59.     if (TRACE) fprintf(stderr,
  60.                        "HTList: Trying to add object %p to a nonexisting list\n",
  61.                newObject);
  62. }
  63.  
  64. BOOL HTList_removeObject ARGS2(HTList *,me, void *,oldObject)
  65. {
  66.   if (me) {
  67.     HTList *previous;
  68.     while (me->next) {
  69.       previous = me;
  70.       me = me->next;
  71.       if (me->object == oldObject) {
  72.     previous->next = me->next;
  73.     free (me);
  74.     return YES;  /* Success */
  75.       }
  76.     }
  77.   }
  78.   return NO;  /* object not found or NULL list */
  79. }
  80.  
  81. void * HTList_removeLastObject ARGS1 (HTList *,me)
  82. {
  83.   if (me && me->next) {
  84.     HTList *lastNode = me->next;
  85.     void * lastObject = lastNode->object;
  86.     me->next = lastNode->next;
  87.     free (lastNode);
  88.     return lastObject;
  89.   } else  /* Empty list */
  90.     return NULL;
  91. }
  92.  
  93. void * HTList_removeFirstObject ARGS1 (HTList *,me)
  94. {
  95.   if (me && me->next) {
  96.     HTList * prevNode;
  97.     void *firstObject;
  98.     while (me->next) {
  99.       prevNode = me;
  100.       me = me->next;
  101.     }
  102.     firstObject = me->object;
  103.     prevNode->next = NULL;
  104.     free (me);
  105.     return firstObject;
  106.   } else  /* Empty list */
  107.     return NULL;
  108. }
  109.  
  110. int HTList_count ARGS1 (HTList *,me)
  111. {
  112.   int count = 0;
  113.   if (me)
  114.     while (me = me->next)
  115.       count++;
  116.   return count;
  117. }
  118.  
  119. int HTList_indexOf ARGS2(HTList *,me, void *,object)
  120. {
  121.   if (me) {
  122.     int position = 0;
  123.     while (me = me->next) {
  124.       if (me->object == object)
  125.     return position;
  126.       position++;
  127.     }
  128.   }
  129.   return -1;  /* Object not in the list */
  130. }
  131.  
  132. void * HTList_objectAt ARGS2 (HTList *,me, int,position)
  133. {
  134.   if (position < 0)
  135.     return NULL;
  136.   if (me) {
  137.     while (me = me->next) {
  138.       if (position == 0)
  139.     return me->object;
  140.       position--;
  141.     }
  142.   }
  143.   return NULL;  /* Reached the end of the list */
  144. }
  145.